package com.heather.exam.controller.admincontroller;// AdminController.java

import com.heather.exam.models.CategoryDTO;
import com.heather.exam.models.QuestionDTO;
import com.heather.exam.pojo.table.User;
import com.heather.exam.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@RestController
@RequestMapping("api/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/createCategory")
    public ResponseEntity<?> createCategory(@RequestBody CategoryDTO categoryDTO,
                                            @RequestParam String questionBankId,
                                            @RequestParam String projectName,
                                            @RequestParam int numberOfQuestions) {
        // 创建一个新的子项目对象
        ProjectDTO projectDTO = new ProjectDTO();
        projectDTO.setQuestionBankId(questionBankId);
        projectDTO.setProjectName(projectName);
        projectDTO.setNumberOfQuestions(numberOfQuestions);

        // 将子项目对象添加到类别DTO对象中
        categoryDTO.setProject(projectDTO);

        // 调用adminService的createCategory方法，传递更新后的categoryDTO对象
        adminService.createCategory(categoryDTO);

        // 返回成功的响应
        return ResponseEntity.ok("Category and project created successfully.");
    }

    @PostMapping("/updateQuestion")
    public ResponseEntity<?> updateQuestion(@RequestBody QuestionDTO questionDTO) {
        adminService.updateQuestion(questionDTO);
        return ResponseEntity.ok("Question updated successfully.");
    }

    // 数据库连接信息
    private static final String DB_URL = "jdbc:mysql://localhost:3306/your_database";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "123456";

    @Transactional
    @PostMapping("/addAnnouncement")
    public void addAnnouncement(String content) {
        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            String sql = "INSERT INTO announcements (content) VALUES (?)";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, content);
                pstmt.executeUpdate();
                System.out.println("公告添加成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    //    public ResponseEntity<?> addAnnouncement(@RequestBody AnnouncementDTO announcementDTO) {
//        adminService.addAnnouncement(announcementDTO);
//        return ResponseEntity.ok("Announcement added successfully.");
//    }
    @PostMapping("/editAnnouncement")
    // 删除公告方法
    public void deleteAnnouncement(int announcementId) {
        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            String sql = "DELETE FROM announcements WHERE id = ?";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setInt(1, announcementId);
                int rowsAffected = pstmt.executeUpdate();
                if (rowsAffected > 0) {
                    System.out.println("公告删除成功！");
                } else {
                    System.out.println("未找到对应的公告！");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
//    public ResponseEntity<?> editAnnouncement(@RequestBody AnnouncementDTO announcementDTO) {
//        adminService.editAnnouncement(announcementDTO);
//        return ResponseEntity.ok("Announcement edited successfully.");
//    }

    @DeleteMapping("/deleteQuestion/{questionId}")
    public ResponseEntity<?> deleteQuestion(@PathVariable Long questionId) {
        adminService.deleteQuestion(questionId);
        return ResponseEntity.ok("Question deleted successfully.");
    }

    @PostMapping("/freezeAccount/{userId}")
    public void freezeAccount(@PathVariable Long userId) {
        User user = findUserById(userId);
        if (user != null) {
            user.unfreezeAccount();
        } else {
            System.out.println("找不到ID为 " + userId + " 的用户。");
        }
    }
    private User findUserById(Object userId) {
        return null;
    }

//        adminService.freezeAccount(userId);
//        return ResponseEntity.ok("User account frozen successfully.");

        @PostMapping("/unfreezeAccount/{userId}")
        public ResponseEntity<?> unfreezeAccount (@PathVariable Long userId){
            adminService.unfreezeAccount(userId);
            return ResponseEntity.ok("User account unfrozen successfully.");
        }

       public class ProjectDTO {
            private String questionBankId;
            private String projectName;
            private int numberOfQuestions;

            public void setQuestionBankId(String questionBankId) {
                this.questionBankId = questionBankId;
            }

            public void setProjectName(String projectName) {
                this.projectName = projectName;
            }

            public void setNumberOfQuestions(int numberOfQuestions) {
                this.numberOfQuestions = numberOfQuestions;
            }
        }
    }
